package com.jason.kotlin.gank.ui.adapter

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.db.GankDaoManager
import com.jason.kotlin.gank.model.GankDataBean
import com.jason.library.widget.snackbar.MySnackbar
import com.ldoublem.thumbUplib.ThumbUpView

class GankItemAdapter : BaseQuickAdapter<GankDataBean, BaseViewHolder>(R.layout.gank_item_article), LoadMoreModule {

    override fun convert(holder: BaseViewHolder, item: GankDataBean) {
        item.apply {
            holder.setText(R.id.tvShowWho, author)
            holder.setText(R.id.tvShowTitle, title)
            holder.setText(R.id.tvShowSubTitle, desc)

            if (!TextUtils.isEmpty(publishedAt) && publishedAt.length > 10) {
                holder.setText(R.id.tvShowTitle, publishedAt.substring(0, 10))
            }

            //图片展示
            var imageUrl = ""
            val images: List<String> = images
            if (images != null && images.size > 0) {
                imageUrl = images[0]
            }
            val imageView = holder.getView(R.id.iv_show) as ImageView
            if (TextUtils.isEmpty(imageUrl)) {
                imageView.visibility = View.GONE
            } else {
                imageView.visibility = View.VISIBLE
                val options = RequestOptions().apply {
                    centerCrop()
                    placeholder(R.drawable.pic_gray_bg)
                }
                Glide.with(context)
                    .load(imageUrl)
                    .apply(options)
                    .into(imageView)
            }

            //查询是否存在收藏
            val isCollect: Boolean =
                GankDaoManager.getCollectDao().queryOneCollectByID(id)
            val collectBtn = holder.getView<ThumbUpView>(R.id.btn_collect)
            if (isCollect) {
                collectBtn.setLike()
            } else {
                collectBtn.setUnlike()
            }

            val tvShowTime = holder.getView<TextView>(R.id.tvShowTime)
            collectBtn.setOnThumbUp(ThumbUpView.OnThumbUp { like ->
                if (like) {
                    val insertResult: Boolean =
                        GankDaoManager.getCollectDao().insertOneCollect(item)
                    if (insertResult) {
                        MySnackbar.makeSnackBarBlack(tvShowTime, "收藏成功")
                    } else {
                        collectBtn.setUnlike()
                        MySnackbar.makeSnackBarRed(tvShowTime, "收藏失败")
                    }
                } else {
                    val deleteResult: Boolean =
                        GankDaoManager.getCollectDao().deleteOneCollect(id)
                    if (deleteResult) {
                        MySnackbar.makeSnackBarBlack(tvShowTime, "取消收藏成功")
                    } else {
                        collectBtn.setLike()
                        MySnackbar.makeSnackBarRed(tvShowTime, "取消收藏失败")
                    }
                }
            })
        }

    }
}